﻿using System;
using System.Collections.Generic;
using Common;
using Common.CustomException;

namespace SpecFlowTfsLinker.Entities
{
    public partial class TestCase
    {
        public class BuilderTestCase
        {
            private readonly TestCase _testCase;

            internal BuilderTestCase()
            {
                _testCase = new TestCase { Given = new List<Statement>(), When = new List<Statement>(), Then = new List<Statement>() };
            }

            public BuilderTestCase Given(string givenStatement)
            {
                _testCase.Given.Add(Statement.Given(givenStatement));
                return this;
            }

            public BuilderTestCase When(string whenStatement)
            {
                _testCase.When.Add(Statement.When(whenStatement));
                return this;
            }

            public BuilderTestCase Then(string thenStatement)
            {
                _testCase.Then.Add(Statement.Then(thenStatement));
                return this;
            }

            public BuilderTestCase Name(string testCaseName)
            {
                _testCase.Name = testCaseName;
                return this;
            }

            public TestCase Build()
            {
                if (!TestCaseNameIsProvided())
                    throw new TestCaseException(Resource.TEST_CASE_BUILD_WITHOUT_NAME);

                if (!GivenWhenAndThenStatementsAreProvided())
                    throw new TestCaseException(Resource.TEST_CASE_BUILD_G_W_T);

                return _testCase;
            }

            private bool TestCaseNameIsProvided()
            {
                return !String.IsNullOrEmpty(_testCase.Name);
            }

            private bool GivenWhenAndThenStatementsAreProvided()
            {
                return _testCase.Given.Count > 0 && _testCase.When.Count > 0 && _testCase.Then.Count > 0;
            }
        }
    }
}